﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection.Emit;
using System.Reflection;
using System.Linq.Expressions;

namespace Project.Common
{
	public class ReflectionTools
	{
		/// <summary>
		/// 排序列，根据类名和字段名返回：例: x=>x.Name
		/// </summary>
		/// <typeparam name="T">类名</typeparam>
		/// <param name="propertyName">字段名</param>
		/// <returns>x=>x.Name</returns>
		public static Expression<Func<T, object>> GetClassPropertyExpression<T>(string propertyName)
		{
			if (string.IsNullOrEmpty(propertyName))
				return null;
			ParameterExpression paramter = Expression.Parameter(typeof(T));
			MemberExpression memberExpression = Expression.Property(paramter, propertyName);
			UnaryExpression unaryExpression = Expression.Convert(memberExpression, typeof(object));
			Expression<Func<T, object>> tempExpression = Expression.Lambda<Func<T, object>>(unaryExpression, paramter);
			return tempExpression;
		}
	}
}
